<?php
namespace App\Service\OrderService\OrderNotifyService;

use App\Service\BaseService;
use App\Model\PssLogModel\MessageCallBackNotifyLog;
use App\Amqp\Producer\WebNotifyMessageProducer;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Amqp\Producer;
use App\Service\Code\Normal;

class BaseNotifyService extends BaseService
{
    protected $user_type;
    protected $message_type;
    protected $message;
    protected $event_sn = null;
    protected $response_to_req;
    protected $receive_mode         = 'mysql';
    const STORAGE_MODE_MYSQL        = 'mysql';
    const STORAGE_MODE_RABBIT_MQ    = 'rabbitmq';
    
    /**
     * 发布消息
     */
    public function addNotifyCallBackLog()
    {
        if( $this->receive_mode == self::STORAGE_MODE_MYSQL ) {
            return $this->addNotifyMessageMysqlStorage();
        } elseif ( $this->receive_mode == self::STORAGE_MODE_RABBIT_MQ ) {
            $result = $this->addNotifyMessageRabbitMqStorage();
            //如果生产消息失败则丢到mysql
            if( $result['code'] != Normal::SUC ) {
                $result = $this->addNotifyMessageMysqlStorage();
            }
            return $result;
        } else {
            return Normal::returnCode(Normal::FAIL,"未实现当前发布消息模式"); 
        }
    }
    
    /**
     * 接收消息入库
     * 1、写入数据库
     * 2、再开启任务去处理数据库数据
     * @param array $param
     */
    public function addNotifyMessageMysqlStorage()
    {
        $response = $this->response_to_req;
        $insert = [
            'event_sn'          => $this->event_sn,
            'user_type'         => $this->user_type,
            'message_type'      => $this->message_type,
            'message'           => $this->message,
            'source'            => 2,
            'response_to_req'   => is_array($response) ? json_encode($response,JSON_UNESCAPED_UNICODE): $response ,
            'client_ip'         => '127.17.0.1'
        ];
        try {
            MessageCallBackNotifyLog::instance()->addLog($insert);
            return Normal::returnCode(Normal::SUC,"写入成功");
        } catch ( \Throwable $th ) {
            logger('info','WebNotifyMessageProducerMysqlError')->info($th->getMessage());
            return Normal::returnCode(Normal::FAIL,$th->getMessage());
        }
    }

    /**
     * 接收消息推送RMQ
     * 1、再由消费端统一入库
     * 2、消费端统一入库之后，再开启任务去处理数据库数据
     * @param array $param
     */
    public function addNotifyMessageRabbitMqStorage()
    {
        $response = $this->response_to_req;
        $notifyMessage = [
            'event_sn'          => $this->event_sn,
            'user_type'         => $this->user_type,
            'message_type'      => $this->message_type,
            'message'           => $this->message,
            'source'            => 1,
            'response_to_req'   => is_array($response) ? json_encode($response,JSON_UNESCAPED_UNICODE): $response ,
            'client_ip'         => '127.17.0.1'
        ];
        try {
            $message    = new WebNotifyMessageProducer($notifyMessage);
            $producer   = ApplicationContext::getContainer()->get(Producer::class);
            $result     = $producer->produce($message,true,30);
            if($result) {
                return Normal::returnCode(Normal::SUC,"写入成功");
            }
            logger('info','WebNotifyMessageProducerRabbitMQError')->info("发布消息失败");
            return Normal::returnCode(Normal::FAIL,"发布消息失败");
        } catch ( \Throwable $th ) {
            logger('info','WebNotifyMessageProducerRabbitMQError')->info($th->getMessage());
            return Normal::returnCode(Normal::FAIL,$th->getMessage());
        }
    }
}